#define _CRT_SECURE_NO_WARNINGS 1

class Solution
{
    int dx[4] = { 1, -1, 0, 0 };
    int dy[4] = { 0, 0, 1, -1 };
    int m, n;
    vector<vector<bool>> vis;
public:
    int numIslands(vector<vector<char>>& grid)
    {
        int ret = 0;
        m = grid.size(), n = grid[0].size();
        vis = vector<vector<bool>>(m, vector<bool>(n));
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (grid[i][j] == '1' && !vis[i][j])
                {
                    dfs(grid, i, j);
                    ret++;
                }
            }
        }
        return ret;
    }
    void bfs(vector<vector<char>>& grid, int a, int b)
    {
        vis[a][b] = true;
        for (int i = 0; i < 4; i++)
        {
            int x = a + dx[i];
            int y = b + dy[i];
            if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == '1')
            {
                dfs(grid, x, y);
            }
        }
    }
};

class Solution
{
    int ret = INT_MIN;
public:
    int maxPathSum(TreeNode* root)
    {
        dfs(root);
        return ret;
    }
    int dfs(TreeNode* root)
    {
        if (!root) return 0;
        int left = dfs(root->left);
        int right = dfs(root->right);
        int tmp = root->val;
        if (left > 0) tmp += left;
        if (right >
            0) tmp += right;
        ret = max(ret, tmp);



        return root->val + max(max(left, right), 0);
    }
};

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
    {
        stack<TreeNode*> st1;
        stack<TreeNode*> st2;

        GetPath(st1, root, p);
        GetPath(st2, root, q);

        int sz1 = st1.size(), sz2 = st2.size();
        int diff = abs(sz1 - sz2);
        if (sz1 > sz2)
        {
            while (diff--) st1.pop();
        }
        else
        {
            while (diff--) st2.pop();
        }

        while (true)
        {
            if (st1.top() != st2.top())
            {
                st1.pop();
                st2.pop();
            }
            else break;
        }
        return st1.top();
    }

    bool GetPath(stack<TreeNode*>& st, TreeNode* root, TreeNode* target)
    {
        if (!root) return false;
        st.push(root);
        if (root == target) return true;
        bool left = GetPath(st, root->left, target);
        if (left) return true;
        bool right = GetPath(st, root->right, target);
        if (right) return true;
        st.pop();
        return false;
    }
};
class Solution
{
    unordered_map<long long, int> hash;
    int target;
    long long sum = 0;
    int ret = 0;
public:
    int pathSum(TreeNode* root, int targetSum)
    {
        hash[0] = 1;
        target = targetSum;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if (!root) return;
        sum += root->val;
        if (hash[sum - target])
            ret += hash[sum - target];
        hash[sum]++;
        dfs(root->left);
        dfs(root->right);
        hash[sum]--;
        sum -= root->val;
    }
};
class Solution
{
    int prei = 0;
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
    {
        return dfs(preorder, inorder, 0, preorder.size() - 1, prei);
    }
    TreeNode* dfs(vector<int>& preorder, vector<int>& inorder, int inbegin, int inend, int& prei)
    {
        if (inbegin > inend) return nullptr;
        int rootval = preorder[prei];
        prei++;
        int i = inbegin;
        for (; i <= inend; i++)
        {
            if (inorder[i] == rootval) break;
        }
        TreeNode* newnode = new TreeNode(rootval);
        newnode->left = dfs(preorder, inorder, inbegin, i - 1, prei);
        newnode->right = dfs(preorder, inorder, i + 1, inend, prei);
        return newnode;
    }
};
